{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "b06cd55f",
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "710c1395",
   "metadata": {},
   "source": [
    "#### 1. 第一题：将np.arange(10)数组中的奇数全部都替换成-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "bf8bb516",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([ 0, -1,  2, -1,  4, -1,  6, -1,  8, -1])"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a1 = np.arange(10)\n",
    "a1[a1%2!=0] = -1\n",
    "a1"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "71047b6d",
   "metadata": {},
   "source": [
    "#### 2. 第二题：有一个4行4列的数组（比如：np.random.randint(0,10,size=(4,4))），请将其中对角线的数取出来形成一个一维数组。提示（使用np.eye）。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "90990846",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 8 8 8]\n",
      " [6 3 5 1]\n",
      " [0 9 0 2]\n",
      " [0 3 0 3]]\n",
      "[0 3 0 3]\n"
     ]
    }
   ],
   "source": [
    "a2 = np.random.randint(0,10,size=(4,4))\n",
    "print(a2)\n",
    "# np.eye(4)\n",
    "eye = np.eye(4,dtype=bool)\n",
    "print(a2[eye])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "3d941002",
   "metadata": {},
   "source": [
    "#### 3. 有一个4行4列的数组，请取出其中(0,0),(1,2),(3,2)的点。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "e281e059",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 4 8 9]\n",
      " [5 4 4 2]\n",
      " [6 8 5 9]\n",
      " [5 1 7 1]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([1, 4, 7])"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a3 = np.random.randint(0,10,size=(4,4))\n",
    "print(a3)\n",
    "a3[[0,1,3],[0,2,2]]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5981becc",
   "metadata": {},
   "source": [
    "#### 4.有一个4行4列的数组，请取出其中2-3行（包括第3行）的所有数据。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "7a067e42",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[5 9 8 4]\n",
      " [0 0 0 0]\n",
      " [3 8 8 7]\n",
      " [9 9 5 7]]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([[3, 8, 8, 7],\n",
       "       [9, 9, 5, 7]])"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a4 = np.random.randint(0,10,size=(4,4))\n",
    "print(a4)\n",
    "a4[2:4]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "10b8d697",
   "metadata": {},
   "source": [
    "##### 5. 有一个8行9列的数组，请将其中1-5行（包含第5行）的第8列大于3的数全部都取出来。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "491273d2",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[0 7 8 3 8 4 4 8 7]\n",
      " [6 1 2 5 4 6 8 6 3]\n",
      " [5 4 5 7 2 4 8 2 1]\n",
      " [6 9 9 3 7 9 6 9 2]\n",
      " [5 6 2 7 5 2 0 1 3]\n",
      " [5 3 7 3 6 7 4 5 9]\n",
      " [0 3 4 1 0 1 6 6 5]\n",
      " [9 5 8 0 2 3 7 7 9]]\n",
      "[3 1 2 3 9]\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "array([9])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a5 = np.random.randint(0,10,size=(8,9))\n",
    "print(a5)\n",
    "a5_son = a5[1:6,8]\n",
    "print(a5_son)\n",
    "a5_son[a5_son>3]"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
